package vn.aptech.scrumgroup.ui;

import java.util.ArrayList;
import java.util.List;

import vn.aptech.scrumgroup.R;
import vn.aptech.scrumgroup.activity.ConceptActivity;
import vn.aptech.scrumgroup.model.AbstractBaseModel;
import vn.aptech.scrumgroup.model.AvatarModel;
import vn.aptech.scrumgroup.model.CircleOnCircleModel;
import vn.aptech.scrumgroup.model.CircleWithTextModel;
import vn.aptech.scrumgroup.model.OnCircle;
import vn.aptech.scrumgroup.resource.Resources;
import vn.aptech.scrumgroup.util.MenuUtil;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import demo.axitdn.event.TouchListener;
import demo.axitdn.physic.Unit;

public class RoleView extends View{
	
	List<OnCircle> objectOnCircleList;
	List<AbstractBaseModel> modelList;
	CircleWithTextModel scrumTeamCircle;
	CircleOnCircleModel scrumMasterCircle;
	CircleOnCircleModel poCircle;
	CircleOnCircleModel teamCircle;
	CircleWithTextModel middleCircle;
	CircleWithTextModel outerCircle;
	Intent intent;
	Context context;
	private float x, y;
	AvatarModel scrumMasterAvar, poAvar, teamAvar;

	public RoleView(Context context, int screenWidth, int screenHeight) {
		super(context);
		this.context = context;
		this.x = screenWidth / 2;
		this.y = screenHeight / 2;
		// TODO Auto-generated constructor stub
		modelList = new ArrayList<AbstractBaseModel>();	
		objectOnCircleList = new ArrayList<OnCircle>();
		init();
	}//end constructor

	private void init() {
		// TODO Auto-generated method stub
		intent = new Intent();
		
		MenuUtil menu = new MenuUtil(context);
		modelList.add(menu);
		
		outerCircle = new CircleWithTextModel(x, y, 102*Unit.i, false, 0, "", 15*Unit.i, Color.rgb(179, 197, 227), Color.WHITE);
//		outerCircle.getCirclePaint().setARGB(255, 179, 197, 227);
		modelList.add(outerCircle);
		middleCircle = new CircleWithTextModel(x, y, 90*Unit.i, false, 0, "", 15*Unit.i, Color.WHITE, Color.WHITE);
//		middleCircle.getCirclePaint().setColor(Color.WHITE);
		modelList.add(middleCircle);
		
		scrumMasterCircle = new CircleOnCircleModel(middleCircle, 4*Unit.i, (float)-Math.PI/2, 30*Unit.i, false, 0, "Scrum Master", 5*Unit.i, false, Color.rgb(51, 122, 162), Color.WHITE, -1);
//		scrumMasterCircle.getCirclePaint().setARGB(255, 51, 122, 162);//blue
//		scrumMasterCircle.getTextPaint().setColor(Color.WHITE);
//		scrumMasterCircle.getTextPaint().setTextSize(11*Unit.i);
//		scrumMasterCircle.getTextPaint().setStyle(Style.FILL_AND_STROKE);
//		scrumMasterCircle.setCharWidth(1*Unit.i);
		scrumMasterCircle.setContext(this.getContext());
		scrumMasterCircle.addTouchListener(new TouchListener() {
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stub
				if(scrumMasterCircle.getPressState()){
					scrumMasterCircle.isBrilliant = false;
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						Bundle scrumMasterBundle = new Bundle();
						scrumMasterBundle.putString("resourceId", Resources.SCRUM_MASTER);
						scrumMasterBundle.putString("title", context.getResources().getString(R.string.SCRUM_MASTER));
						intent.replaceExtras(scrumMasterBundle);
						intent.setClass(context, ConceptActivity.class);
						context.startActivity(intent);
					}
					scrumMasterCircle.setPressState(false);
				}
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(scrumMasterCircle.isPointOf(x, y)){
					scrumMasterCircle.isBrilliant = true;
					this.x = x;
					this.y = y;
					scrumMasterCircle.setPressState(true);
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				if(scrumMasterCircle.getPressState()){
					scrumMasterCircle.moveTo(x, y);
				}
				return false;
			}
		});
		
		modelList.add(scrumMasterCircle);
		
		poCircle = new CircleOnCircleModel(middleCircle, 4*Unit.i, (float)Math.PI/6, 30*Unit.i, false, 0, "Product Owner", 5*Unit.i, false, Color.rgb(51, 122, 162), Color.WHITE, -1);
//		poCircle.getCirclePaint().setARGB(255, 51, 122, 162);
//		poCircle.getTextPaint().setTextSize(11*Unit.i);
//		poCircle.getTextPaint().setColor(Color.WHITE);
//		poCircle.getTextPaint().setStyle(Style.FILL_AND_STROKE);
//		poCircle.setCharWidth(1*Unit.i);
		poCircle.setContext(this.getContext());
		poCircle.addTouchListener(new TouchListener() {
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stub
				if(poCircle.getPressState()){
					poCircle.isBrilliant = false;
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						Bundle poBundle = new Bundle();
						poBundle.putString("resourceId", Resources.PRODUCT_OWNER);
						poBundle.putString("title", context.getResources().getString(R.string.PRODUCT_OWNER));
						intent.replaceExtras(poBundle);
						intent.setClass(context, ConceptActivity.class);
						context.startActivity(intent);
					}
					poCircle.setPressState(false);
				}
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(poCircle.isPointOf(x, y)){
					poCircle.isBrilliant = true;
					this.x = x;
					this.y = y;
					poCircle.setPressState(true);
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				if(poCircle.getPressState()){
					poCircle.moveTo(x, y);
				}
				return false;
			}
		});
		modelList.add(poCircle);
		
		teamCircle = new CircleOnCircleModel(middleCircle, 4*Unit.i, (float)Math.PI*5/6, 30*Unit.i, false, 0, "Team", 5*Unit.i, false, Color.rgb(51, 122, 162), Color.WHITE, -1);
//		teamCircle.getCirclePaint().setARGB(255, 51, 122, 162);
//		teamCircle.getTextPaint().setTextSize(11*Unit.i);
//		teamCircle.getTextPaint().setColor(Color.WHITE);
//		teamCircle.getTextPaint().setStyle(Style.FILL_AND_STROKE);
		teamCircle.setCharWidth(1*Unit.i);
		teamCircle.setContext(this.getContext());
		teamCircle.addTouchListener(new TouchListener() {
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stub
				if(teamCircle.getPressState()){
					teamCircle.isBrilliant = false;
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						Bundle teamBundle = new Bundle();
						teamBundle.putString("resourceId", Resources.DEVELOPMENT_TEAM);
						teamBundle.putString("title", context.getResources().getString(R.string.DEVELOPMENT_TEAM));
						intent.replaceExtras(teamBundle);
						intent.setClass(context, ConceptActivity.class);
						context.startActivity(intent);
					}
					teamCircle.setPressState(false);
				}
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(teamCircle.isPointOf(x, y)){
					teamCircle.isBrilliant = true;
					this.x = x;
					this.y = y;
					teamCircle.setPressState(true);
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				if(teamCircle.getPressState()){
					teamCircle.moveTo(x, y);
				}
				return false;
			}
		});
		
		modelList.add(teamCircle);
		
		scrumTeamCircle = new CircleWithTextModel(x, y, 40*Unit.i,  false, 0, "Scrum Roles", 7*Unit.i, Color.rgb(51, 122, 162), Color.WHITE);
//		scrumTeamCircle.getCirclePaint().setColor(Color.RED);
//		scrumTeamCircle.getTextPaint().setTextSize(15*Unit.i);
//		scrumTeamCircle.getTextPaint().setColor(Color.WHITE);
//		scrumTeamCircle.getTextPaint().setStyle(Style.FILL_AND_STROKE);
//		scrumTeamCircle.setCharWidth(1);
//		scrumTeamCircle.getCirclePaint().setARGB(255, 51, 122, 162);	
		scrumTeamCircle.setContext(this.getContext());
		scrumTeamCircle.addTouchListener(new TouchListener() {
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stubteamCircle.isBrilliant = false;
				if(scrumTeamCircle.getPressState()){
					scrumTeamCircle.isBrilliant = false;
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						Bundle scrumTeamBundle = new Bundle();
						scrumTeamBundle.putString("resourceId", Resources.SCRUM_TEAM);
						scrumTeamBundle.putString("title", context.getResources().getString(R.string.SCRUM_TEAM));
						intent.replaceExtras(scrumTeamBundle);
						intent.setClass(context, ConceptActivity.class);
						context.startActivity(intent);
					}
					scrumTeamCircle.setPressState(false);
				}
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(scrumTeamCircle.isPointOf(x, y)){
					scrumTeamCircle.isBrilliant = true;
					this.x = x;
					this.y = y;
					scrumTeamCircle.setPressState(true);
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				return false;
			}
		});

		modelList.add(scrumTeamCircle);
		
		objectOnCircleList.add(scrumMasterCircle);
		objectOnCircleList.add(poCircle);
		objectOnCircleList.add(teamCircle);
		for (OnCircle model:objectOnCircleList){
			for (int i=0;i<objectOnCircleList.size();i++)
				if (model!=objectOnCircleList.get(i))
					model.addMoveRoundListener(objectOnCircleList.get(i));
		}//end for
		
		//avatar
		scrumMasterAvar = new AvatarModel(scrumMasterCircle, scrumTeamCircle, 30*Unit.i, 30*Unit.i, 1.8f, false, Color.rgb(51, 122, 162));
//		scrumMasterAvar.paint.setARGB(255, 51, 122, 162);
		modelList.add(scrumMasterAvar);
		poAvar = new AvatarModel(poCircle, scrumTeamCircle, 30*Unit.i, 30*Unit.i, 1.8f, false, Color.rgb(179, 37, 38));
//		poAvar.paint.setARGB(255, 179, 37, 38);
		modelList.add(poAvar);
		teamAvar = new AvatarModel(teamCircle, scrumTeamCircle, 30*Unit.i, 30*Unit.i, 1.8f, true, Color.rgb(40, 131, 55));
//		teamAvar.paint.setARGB(255, 40, 131, 55);
		modelList.add(teamAvar);
	}//end init

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		canvas.drawColor(Color.WHITE);
		for (AbstractBaseModel model : modelList){
			model.onDraw(canvas);
		}//end for
	}//end onDraw
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
//		int action = event.getAction();
		for(AbstractBaseModel model : modelList){
			model.fireTouchEvent(event);
		}
		this.invalidate();
		return true;
	}//end onTouch
}//end class
